package com.hdc.hdc.warehouseManage.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.hdc.hdc.warehouseManage.entity.GoodsTrade;
import com.hdc.hdc.warehouseManage.entity.vo.WareHouseVO;
import com.hdc.hdc.warehouseManage.service.WareHouseAndPositionServiceI;
import com.hdc.hdc.warehouseManage.service.WareHouseServiceI;
import com.hdc.hdc.warehouseManage.utill.AjaxReturnVO;

/**
 * 仓库管理
 * @author GJY
 * 人生格言：学以致用终将成功
 * @version 创建时间：2018年4月16日 上午9:18:27 
 *
 */
@RestController
@RequestMapping(value="/wareHouseController")
public class WareHouseController {
	
	@Autowired
	private WareHouseServiceI wareHouseServiceI;
	
	@Autowired
	private WareHouseAndPositionServiceI wareHouseAndPositionServiceI;
	
	@RequestMapping(value="/goodsTrade")
	public ModelAndView gotoPageForGoodsTrade() {
		return new ModelAndView("/warehouse/goods_trade");
	}
	
	@RequestMapping(value="/goodsLocationDesc")
	public ModelAndView gotoPageForLocation() {
		
		return new ModelAndView("/warehouse/locationDesc");
	}
	
	@RequestMapping(value="/warehouseManage")
	public ModelAndView gotoPageForWareHouse() {
		
		return new ModelAndView("/warehouse/warehouse");
	}
	
	@RequestMapping(value="/getGoodsTrade")
	public AjaxReturnVO getGoodsTradeForPage(@RequestParam(value="pageNum")Integer pageNum,@RequestParam(value="pageSize")Integer pageSize) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			Page<GoodsTrade> goodsTrades = this.wareHouseServiceI.findWareHouseForPage(pageNum,pageSize);
			j.setOk(true);
			
			Map<String, Object> attrs = new HashMap<String, Object>();
			attrs.put("totalSize", goodsTrades.getTotalElements());
			attrs.put("val", goodsTrades.getContent());
			j.setAttrs(attrs);
			
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
			j.setMsg("查询失败");
		}
		
		return j;
	}
	
	@PostMapping(value="/getGoodsTradeForQuery")
	public AjaxReturnVO getGoodsTradeForQueryParam(@RequestBody WareHouseVO vo) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			
			j.setOk(true);
			j.setData(this.wareHouseServiceI.queryByVo(vo));
			
		}catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
			j.setMsg("查询失败:"+e.getMessage());
		}
		
		return j;
	}
	
	@GetMapping(value="/goodsLocationSetPage")
	public ModelAndView gotoLocationSetPage(){
		
		return new ModelAndView("/warehouse/goods_location");
	}
	
	@GetMapping(value="/initAllQueryMessage")
	public AjaxReturnVO initAllQuery() {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			vo.setAttrs(this.wareHouseAndPositionServiceI.getHouseAndLocations());
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("初始化失败"+e.getMessage());
			vo.setOk(false);
		}
		
		return vo;
	}
	
	
}
